﻿using System;
using System.Collections.Generic;
using System.Text;
using Zhuang.Utility.Common.DataBase;
using System.Data;

namespace Zhuang.DAL.Access
{
    public class MaxNumberDAL
    {
        public string GetMaxNumber(string prefix)
        {
            return GetMaxNumber(prefix, "");

        }

        public string GetMaxNumber(string prefix, string midFlag)
        {
            
            string strSql = @"select count(*) from MaxNumber where Prefix='{0}' and MidFlag ='{1}' ";
            strSql = string.Format(strSql,prefix,midFlag);

            int count=Convert.ToInt32(OleDbHelper.Query(strSql).Tables[0].Rows[0][0].ToString());
            if (count > 0)
            {
                strSql = @"update MaxNumber set [Number]=[Number]+1 where Prefix='{0}' and MidFlag ='{1}'";
            }
            else
            {
                strSql = @"insert into MaxNumber(Prefix,MidFlag,[Number]) values('{0}','{1}',1)";
                
            }

            strSql = string.Format(strSql, prefix, midFlag);

            OleDbHelper.ExecuteSql(strSql);

            strSql = @"select Prefix,MidFlag,Number from MaxNumber where Prefix='{0}' and MidFlag ='{1}'";
            strSql = string.Format(strSql,prefix,midFlag);

            DataTable dt = OleDbHelper.Query(strSql).Tables[0];

            return dt.Rows[0]["Prefix"].ToString().Trim() + dt.Rows[0]["MidFlag"].ToString().Trim() + dt.Rows[0]["Number"].ToString().Trim().PadLeft(6,'0');

        }
    }
}
